package com.hs.pk.berth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hs.common.api.dto.ResultCode;
import com.hs.common.api.dto.ResultVo;
import com.hs.pk.berth.bean.Berth;
import com.hs.pk.berth.bean.Road;
import com.hs.pk.berth.dto.BerthDto;
import com.hs.pk.berth.dto.FindBerthDto;
import com.hs.pk.berth.exception.Asserts;
import com.hs.pk.berth.service.IBerthService;
import com.hs.pk.berth.service.IRoadService;
import com.hs.pk.berth.vo.FindRoadVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 路段 前端控制器
 * </p>
 *
 * @author lhj
 * @since 2022-07-20
 */
@RestController
@RequestMapping("/road")
@Api(tags = "路段模块")
@Slf4j
public class RoadController {
    @Autowired
    private IRoadService roadService;
    @Autowired
    private IBerthService berthService;

    @ApiOperation("条件分页查询路段列表")
    @PostMapping("/window/{current}/{size}")
    public ResultVo findRoad(
            @PathVariable @ApiParam(name = "current", value = "当前页数") Integer current,
            @PathVariable @ApiParam(name = "size", value = "页面大小") Integer size,
            @RequestBody(required = false) @ApiParam(name = "findBerthDto", value = "泊位条件查询对象") FindBerthDto findBerthDto) {
        System.out.println("findBerthDto = " + findBerthDto);
        return roadService.findRoadByPage(current, size, findBerthDto);
    }

    @ApiOperation("根据路段编号修改路段信息")
    @PutMapping("/window")
    public ResultVo updateRoad(@RequestBody @ApiParam(name = "road", value = "路段对象") Road road) {
        boolean b = roadService.updateById(road);
        if (b) {
            return new ResultVo(ResultCode.SUCCESS);
        }
        return new ResultVo(ResultCode.FAILED);
    }

    @ApiOperation("新增路段信息")
    @PostMapping("/window")
    public ResultVo addRoad(@RequestBody @ApiParam(name = "road", value = "路段对象") Road road) {
        boolean save = roadService.save(road);
        if (save) {
            return new ResultVo(ResultCode.SUCCESS);
        }
        return new ResultVo(ResultCode.FAILED);
    }

    @ApiOperation("根据路段编号删除指定路段信息")
    @DeleteMapping("/window/{id}")
    public ResultVo deleteRoad(@PathVariable @ApiParam(name = "id", value = "路段Id") Long id) {
        //1. 判断该路段下有无泊车位
        LambdaQueryWrapper<Berth> qw = Wrappers.lambdaQuery(Berth.class);
        qw.eq(Berth::getRoadId, id);
        List<Berth> list = berthService.list(qw);
        //1.1 存在泊位不能删除路段，抛出异常
        if (!CollectionUtils.isEmpty(list)) {
            Asserts.failed("该路段下已存在泊位，不能移除！");
        }

        //2. 删除路段
        boolean b = roadService.removeById(id);
        if (b) {
            return new ResultVo(ResultCode.SUCCESS);
        }
        return new ResultVo(ResultCode.FAILED);
    }

    @ApiOperation("根据路段名称查找位置、收费类型、空闲车位")
    @GetMapping("/searchByRoadName")
    public BerthDto searchBerthDtoByRoadName(
            @RequestParam("roadName") @ApiParam("路段名称") String roadName) {
        return roadService.searchBerthDtoByRoadName(roadName);
    }

    @ApiOperation("查询所有路段名称和编号")
    @GetMapping("/findAllRoad")
    public List<FindRoadVo> findAllRoad() {
        LambdaQueryWrapper<Road> qw = Wrappers.lambdaQuery(Road.class);
        qw.select(Road::getName, Road::getId);
        List<FindRoadVo> vos = new ArrayList<>();
        List<Road> list = roadService.list(qw);
        list.forEach(e -> {
            FindRoadVo vo = new FindRoadVo();
            BeanUtils.copyProperties(e, vo);
            vos.add(vo);
        });
        return vos;
    }


}
